package com.vodone.v1pin.component.container
{
	import flash.events.Event;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.ResizeEvent;
	
	public class SaleContainer extends Container
	{
		public function SaleContainer()
		{
			super();
			this.horizontalScrollPolicy = "off";
			this.verticalScrollPolicy = "off";
			addEventListener( ResizeEvent.RESIZE , resizeHandler );
		}
		private function resizeHandler(e:Event):void
		{
			this.measure();
		}
		
		public var columnWidth:Number = 100;
		public var verticalGap:Number = 1;
		public var horizontalGap:Number = 1;
		public var columnCount:Number = 4;
		
		/**
		 * 键为列索引，值为每一列布局的y值 
		 */		
		private var yDict:Dictionary = new Dictionary();
		/**
		 * 当前布局的行索引 
		 */		
		private var currentRowIndex:int = -1;
		/**
		 * 当前布局的列索引 
		 */		
		private var currentColumnIndex:int = 0;
		private var columnIndexArr:Array = [];
		private var padding:int = 0;
		
		protected override function measure():void
		{
			super.measure();
			var start:Number = getTimer();
			columnCount = Math.floor( width / ( columnWidth + horizontalGap  ) );
			padding = ( width - columnCount *( columnWidth + horizontalGap ) + horizontalGap) /2;
			// 初始化每一列的y
			columnIndexArr = [];
			currentRowIndex = -1;
			for( var i:int = 0 ; i < columnCount ; i++ )
			{
				yDict[ i  ] = 0;
				columnIndexArr.push( i );
			}
			var childArr:Array = this.getChildren().reverse();
			// 布局每一行
			while( childArr.length > 0 )
			{
				currentRowIndex ++;
				// 每次挑出一组
				var currentArr:Array = childArr.splice( Math.max( 0 , childArr.length - columnCount ) , 
					Math.min( childArr.length , columnCount) );
				var sortArr:Array = currentArr.concat();
				if( currentRowIndex > 0 )
				{
					// 给数组排序，将height值大的拍到最后面
					sortArr.sort( sortFunc  );
					// 给每一列排序，将y小的排到最前面
					columnIndexArr.sort( sortColumns );
				}
				for( var j:int = 0 ; j < columnIndexArr.length ; j++  )
				{
					var index:int = columnIndexArr[j] as int;
					var ui:UIComponent = sortArr.pop() as UIComponent;
					if( ui != null )
					{
						var x:Number = getX( index );
						var y:Number = yDict[ index ];
						ui.move( x , y );
						yDict[ index ] += ( ui.height + verticalGap );
						if( j < columnIndexArr.length - 1 && layOutNextRow( index , columnIndexArr[ j + 1 ]  as int ) )
						{
							childArr = childArr.concat( sortArr );
							break;
						}
					}
				}
			}
			var end:Number = getTimer();
			trace("布局" + numChildren + "个元素使用时间:" + ( end - start ));
			
			setHeight();
		}
		
		private function setHeight():void
		{
			var h:Number = 0;
			for each( var y:Number in yDict)
			{
				if( y > h )
				{
					h = y;
				}
			}
			this.measuredHeight = h;
		}
		
		protected override function updateDisplayList(w:Number, h:Number):void
		{
			super.updateDisplayList( w , h );
			
		}
		
		private function layOutNextRow( currentColumn:int , nextColumn:int ):Boolean
		{
			return yDict[ currentColumn ] - yDict[ nextColumn ] < 0;
		}
		private function getX( columnIndex:int ):int
		{
			return padding + ( columnWidth + horizontalGap ) * columnIndex;
		}
		
		private function sortColumns( a:int , b:int ):int
		{
			return yDict[a] - yDict[b];
		}
		private function sortFunc( a:Object , b:Object  ):int
		{
			return a["height"] - b["height"];
		}
	}
}